//
//  MOOfficeTextSearchViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/10.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOOfficeTextSearchViewController.h"
#import "MOOfficeTextSearchViewCell.h"
#import "MONoticeViewController.h"

@interface MOOfficeTextSearchViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *footerView;

//文件字数组
@property(nonatomic,strong)NSArray *textCharArray;
//部门数组
@property(nonatomic,strong)NSArray *sectionArray;

@end

@implementation MOOfficeTextSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    [self getData];
}

-(void)creatView
{
    self.title = @"公文查询";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableFooterView = self.footerView;
    
}

-(void)initData
{
    
    
    self.dataSource = [NSMutableArray array];
    NSArray *arr1 = @[
          @[@"标题"],
          @[@"文件字",@"文件年份",@"文件号"],
          @[@"文件类型"],
          @[@"拟稿部门"]
          ];
    NSArray *typeArr = @[
                      @[@"1"],
                      @[@"2",@"2",@"1"],
                      @[@"2"],
                      @[@"2"]
                      ];
    for (int i = 0; i<arr1.count; i++) {
        NSMutableArray *muArr = [NSMutableArray array];
        NSArray *arr2 = arr1[i];
        NSArray *arr3 = typeArr[i];
        for (int j = 0; j<arr2.count; j++) {
            MOOfficeTextSearchModel *model = [[MOOfficeTextSearchModel alloc]init];
            model.nameStr = arr2[j];
            model.typeStr = arr3[j];
            if (i==1&&j==1) {
                model.valueStr = [NSString getCurrentTimesAndFormat:@"YYYY"];
            }
            
            [muArr addObject:model];
        }
        [self.dataSource addObject:muArr];
    }
    [self.tableView reloadData];
}

-(void)getData
{
    //公文查询文件字
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ODPSCX_WJZLIST_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"公文查询文件字:%@",responseObject);
        if ([responseObject isKindOfClass:[NSArray class]]) {
            self.textCharArray = responseObject;
        }
        
    } failure:^(NSError *error) {
        
        NSLog(@"公文查询文件字:%@",[error localizedDescription]);
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
    
    //公文查询主办部门名称
    NSString *postUrl1 = [NSString stringWithFormat:@"%@?authorization=%@",ODPSCX_GETZBBMMC_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl1 parameters:@{} success:^(id responseObject) {
        NSLog(@"公文查询主办部门名称:%@",responseObject);
        
        if ([responseObject[@"result"] isKindOfClass:[NSArray class]]) {
            self.sectionArray = responseObject[@"result"];
        }
        
    } failure:^(NSError *error) {
        
        NSLog(@"公文查询主办部门名称:%@",[error localizedDescription]);
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

-(void)clickSubMitButton
{
    MONoticeViewController *vc = [[MONoticeViewController alloc]init];
    vc.typeStr = @"3";
    vc.searchConditionArray = self.dataSource;
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSource[section];
    
    return arr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOOfficeTextSearchViewCell *cell = [MOOfficeTextSearchViewCell cellWithTableView:tableView andIndexPath:indexPath];
//    __weak typeof (self)weakSelf = self;
    MOOfficeTextSearchModel *model = self.dataSource[indexPath.section][indexPath.row];
    cell.inputEndBlock = ^(NSString * _Nonnull text) {
        model.valueStr = text;
    };
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOOfficeTextSearchModel *model = self.dataSource[indexPath.section][indexPath.row];
    if ([model.typeStr integerValue]==2) {
        
        if (indexPath.section==1) {
            if (indexPath.row==0) {
                SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
                NSArray *tArr = self.textCharArray;
                pickview.numberArray = tArr;
                __weak typeof (self)weakSelf = self;
                pickview.clickIndexBlock = ^(NSInteger index) {
                    model.valueStr = tArr[index];
                    [weakSelf.tableView reloadData];
                };
                [WINDOW addSubview:pickview];
            }else{
                
                XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
                pickerDate.maxComponentCount = 1;
                pickerDate.xlsn0wDelegate = self;
                [pickerDate show];
            }
        }else if (indexPath.section==2){
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"发文",@"收文"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else{
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = self.sectionArray;
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }
    }
}



- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    NSString *selectD = [NSString stringWithFormat:@"%ld", (long)year];
    if ([selectD integerValue]>[[NSString getCurrentTimesAndFormat:@"YYYY"] integerValue]) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"查询时间不能大于当前时间" showInView:self.view];
        return;
    }
    
    NSString *text = [NSString stringWithFormat:@"%ld", (long)year];
    MOOfficeTextSearchModel *model = self.dataSource[1][1];
    model.valueStr = text;
    [self.tableView reloadData];
    
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KGrayColor_F3F3F3;
        _tableView.separatorColor = KGrayColor_F3F3F3;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(130))];
        
        UIButton *subMitButton = [[UIButton alloc]initWithFrame:CGRectMake(kWConversions(15), kHConversions(22), SCREEN_WIDTH-kWConversions(15)*2, kHConversions(43))];
        [subMitButton setBackgroundColor:KBlueColor_2C94FF];
        subMitButton.titleLabel.font = FONT_SIZE(15);
        [subMitButton setTitle:@"提交" forState:UIControlStateNormal];
        subMitButton.layer.masksToBounds = YES;
        subMitButton.layer.cornerRadius = 5.0;
        [subMitButton addTarget:self action:@selector(clickSubMitButton) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:subMitButton];
    }
    return _footerView;
}


@end
